"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : security-overflow
Case Name   : SQL 语句 CREATE FOREIGN TABLE 中的 filename 选项参数是否会造成缓冲区溢出
Create At   : 2023/06/05
@zou_jialiang0501162244
Description :
    1. 创建 file_fdw 扩展
    2. 创建外部服务器
    3. 创建 filename 选项超长的外部表
    4. 清理环境
Expect      :
    1. 成功
    2. 成功
    3. 成功
    4. 成功
History     :
"""

import os
import unittest

from yat.test import Node
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class Overflow(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_node = Node('PrimaryDbUser')
        self.cons = Constant()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.fs_name = 'fs_memory_0007'
        self.table = 't_memory_0007'

    def test_security(self):
        text1 = '-----step1:创建 postgres_fdw 扩展;expect:成功-----'
        self.log.info(text1)
        msg = self.pri_sh.execut_db_sql(
            'create extension if not exists file_fdw;')
        self.log.info(msg)
        self.assertIn(self.cons.create_extension_success, msg, f'执行失败:{text1}')

        text2 = '-----step2:创建外部服务器;expect:成功'
        self.log.info(text2)
        sql = f"drop server if exists {self.fs_name};" \
              f"create server {self.fs_name} foreign data wrapper file_fdw;"
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        self.assertIn(self.cons.create_server_success, msg,
                      f'执行失败：{text2}')

        text3 = '-----step3:创建 filename 选项超长的外部表;expect:成功-----'
        self.log.info(text3)
        filename = 'a' * 16000
        msg = self.pri_sh.execut_db_sql(
            f"create foreign table {self.table}(id int) server {self.fs_name} "
            f"options (filename '{filename}', format 'csv');")
        self.log.info(msg)
        self.assertIn(self.cons.CREATE_FOREIGN_SUCCESS_MSG, msg,
                      f'执行失败:{text3}')
        msg = self.pri_sh.execut_db_sql(f'select * from {self.table};')
        self.log.info(msg)
        self.assertTrue(msg.find('ERROR:') > -1, f'执行失败：{text3}')

    def tearDown(self):
        text4 = '-----step4:清理数据;expect:成功-----'
        self.log.info(text4)
        sql_cmd = f"drop server if exists {self.fs_name} cascade;"
        msg = self.pri_sh.execut_db_sql(sql_cmd)
        self.log.info(msg)
        self.assertIn(self.cons.drop_server_success, msg, f"执行失败:{text4}")
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
